package images;

import Composite.*;
import fichiers.*;
import java.util.*;
import javax.swing.*;
import Thread.*;
import support.*;

public class VignettesListModel extends AbstractListModel implements Observer, InterfaceModel {

    private Dossier modeladapte;
    private ArrayList mesElements;
    private ArrayList vues;

    public VignettesListModel() {

        this.mesElements = new ArrayList();
        this.vues = new ArrayList();
    }

    public Object getElementAt(int index) {

        return mesElements.get(index);

    }

    public int getSize() {

        return mesElements.size();
    }

    public void ajouterElement(Element element) {

        this.mesElements.add(element);
        this.fireIntervalAdded(this, this.getSize(), this.getSize());

    }

    public void ajouterElement(Element element, int index) {

        this.mesElements.add(index, element);
        this.fireIntervalAdded(this, this.getSize(), this.getSize());

    }

    public void supprimerElement(Element element) {

        int index = this.mesElements.indexOf(element);
        this.mesElements.remove(element);
        this.fireIntervalRemoved(this, index, index);

    }

    public void mettreAJourStructure(int index) {

        this.fireContentsChanged(this, index, index);
    }

    public void setModelAdapte(Dossier modelA) {

        if (modelA != null) {

            if (this.modeladapte != null) {
                this.modeladapte.deleteObserver(this);
            }
            this.modeladapte = modelA;
            this.modeladapte.addObserver(this);

            int taille = this.mesElements.size();
            mesElements.clear();
            this.fireIntervalRemoved(this, 0, taille);

            ArrayList elements = this.modeladapte.getDossiers();

            Iterator it = elements.iterator();
            while (it.hasNext()) {

                Element suiv = (Element) it.next();
                this.ajouterElement(suiv);

            }

            ThreadCreationVignette monThread = new ThreadCreationVignette(this);
            monThread.start();
        }
    }
    
    public void removeModel(){
         if (this.modeladapte != null) {
                this.modeladapte.deleteObserver(this);
           
            this.modeladapte = null;

            int taille = this.mesElements.size();
            mesElements.clear();
            this.fireIntervalRemoved(this, 0, taille);
             }
    }

    public Dossier getModelAdapte() {

        return this.modeladapte;
    }

    public void update(Observable o, Object arg) {

        Element el = (Element) arg;
        if (this.modeladapte.getChildCount() > this.mesElements.size()) {

            int index = this.modeladapte.getIndex(el);
            this.ajouterElement(el, index);
            ThreadCreationVignette monThread = new ThreadCreationVignette(this);
            monThread.start();
        } else if (this.modeladapte.getChildCount() < this.mesElements.size()) {

            this.supprimerElement(el);
        }else {

            ThreadCreationVignette monThread = new ThreadCreationVignette(this);
            monThread.start();
        }

    }

    public void addVue(InterfaceVue v) {
        this.vues.add(v);
    }

    public void removeVue(InterfaceVue v) {
        this.vues.remove(v);
    }

    public void notifyVue() {
        Iterator it = this.vues.iterator();
        while (it.hasNext()) {

            InterfaceVue suiv = (InterfaceVue) it.next();
            suiv.update();
        }
    }
}



